package com.qzsoft.zhjd.materiel.modules.materieldata.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.qzsoft.jeemis.common.utils.ConvertDictUtils;
import com.qzsoft.zhjd.common.annotation.ZhjdDataFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qzsoft.zhjd.common.components.BaseServiceComponent;
import com.qzsoft.zhjd.materiel.modules.materieldata.dto.MaterielDataCountDto;
import com.qzsoft.zhjd.materiel.modules.materieldata.dto.MaterielDataCountTypeDto;
import com.qzsoft.zhjd.materiel.modules.materieldata.dto.MaterielDataCountTypeListDto;
import com.qzsoft.zhjd.materiel.modules.materieldata.dto.MaterielDataDetailsDto;
import com.qzsoft.zhjd.materiel.modules.materieldata.dto.MaterielDataForCategoryChartDto;
import com.qzsoft.zhjd.materiel.modules.materieldata.dto.MaterielDataForItemChartDto;
import com.qzsoft.zhjd.materiel.modules.materieldata.dto.MaterielDataListDto;
import com.qzsoft.zhjd.materiel.modules.materieldata.entity.MaterielDataEntity;
import com.qzsoft.zhjd.materiel.modules.materieldata.mapper.MaterielDataMapper;
import com.qzsoft.zhjd.materiel.modules.materieldata.service.MaterielDataService;

/**
 * @ClassName MaterielDataServiceImpl
 * @Description 物料数据业务接口实现
 * @author linhuawei
 * @since 2021-09-01
 */
@Service
public class MaterielDataServiceImpl extends ServiceImpl<MaterielDataMapper, MaterielDataEntity>
        implements MaterielDataService {

    @Autowired
    private BaseServiceComponent baseServiceComponent;

    @Override
    @ZhjdDataFilter(tableAlias = "t1")
    public IPage<MaterielDataListDto> pageDto(Map<String, Object> params) {
        IPage<MaterielDataListDto> page = baseServiceComponent.getPage(params, MaterielDataListDto.class);
        IPage<MaterielDataListDto> dataPage = baseMapper.queryDto(page, params);
        return ConvertDictUtils.formatDicDataPage(dataPage);
    }

    @Override
    public List<MaterielDataListDto> ListDto(Map<String, Object> params) {
        List<MaterielDataListDto> dataList = baseMapper.queryDto(params);
        return ConvertDictUtils.formatDicDataList(dataList);
    }

    @Override
    @ZhjdDataFilter(tableAlias = "t1")
    public MaterielDataCountDto countDto(Map<String, Object> params) {
        return baseMapper.countDto(params);
    }
    
    @Override
    @ZhjdDataFilter(tableAlias = "t1")
    public MaterielDataCountTypeDto countByWeightDataType(Map<String, Object> params) {
        List<MaterielDataCountTypeListDto> list = baseMapper.countByWeightDataType(params);
        list = ConvertDictUtils.formatDicDataList(list);
        
        MaterielDataCountTypeDto dto = new MaterielDataCountTypeDto();
        dto.setList(list);
        dto.count();
        return dto;
    }

    @Override
    public MaterielDataDetailsDto getDetailsById(String id) {
        MaterielDataDetailsDto dto = baseMapper.getDetailsById(id);
        return ConvertDictUtils.formatDicData(dto);
    }

    @Override
    @ZhjdDataFilter(tableAlias = "t1")
    public List<MaterielDataForItemChartDto> countForItemChart(Map<String, Object> params) {
        return baseMapper.countForItemChart(params);
    }

    @Override
    @ZhjdDataFilter(tableAlias = "t1")
    public List<MaterielDataForCategoryChartDto> countForCategoryChart(Map<String, Object> params) {
        return baseMapper.countForCategoryChart(params);
    }

    @Override
    @ZhjdDataFilter(tableAlias = "t1")
    public String getLastDay(Map<String, Object> params) {
        Date lastTime = baseMapper.getLastTime(params);
        String day = null;
        if (null != lastTime) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                day = sdf.format(lastTime);
            } catch (Exception e) {
                day = null;
            }
        }
        return day;
    }

    @Override
    @ZhjdDataFilter(tableAlias = "t1")
    public String getLastMonth(Map<String, Object> params) {
        Date lastTime = baseMapper.getLastTime(params);
        String month = null;
        if (null != lastTime) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
                month = sdf.format(lastTime);
            } catch (Exception e) {
                month = null;
            }
        }
        return month;
    }

    @Override
    @ZhjdDataFilter(tableAlias = "t1")
    public String getLastYear(Map<String, Object> params) {
        Date lastTime = baseMapper.getLastTime(params);
        String year = null;
        if (null != lastTime) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
                year = sdf.format(lastTime);
            } catch (Exception e) {
                year = null;
            }
        }
        return year;
    }

    @Override
    @ZhjdDataFilter(tableAlias = "t1")
    public List<String> getYearList(Map<String, Object> params) {
        return baseMapper.getYearList(params);
    }

}
